package com.ruoyi.web.service.impl;


import com.ruoyi.common.base.chat.domain.Chat;
import com.ruoyi.common.base.chat.domain.ChatMessage;
import com.ruoyi.common.base.chat.dto.ChatEditDTO;
import com.ruoyi.common.base.chat.mapper.ChatMapper;
import com.ruoyi.common.base.chat.mapper.ChatMessageMapper;
import com.ruoyi.common.base.chat.mapstruct.ChatStruct;
import com.ruoyi.common.base.chat.vo.ChatAppVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.customize.util.CommonUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.service.IChatService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 会话Service业务层处理
 *
 * @author gdk
 * @date 2023-05-15
 */
@Service
@AllArgsConstructor
public class ChatServiceImpl implements IChatService {
    private final ChatMapper chatMapper;
    private final ChatMessageMapper chatMessageMapper;

    private final CommonUtil commonUtil;

    /**
     * 查询会话
     *
     * @param id 会话主键
     * @return 会话
     */
    @Override
    public Chat selectChatById(Long id) {
        return chatMapper.selectChatById(id, null);
    }

    /**
     * 查询会话列表
     *
     * @param chat 会话
     * @return 会话
     */
    @Override
    public List<Chat> selectChatList(Chat chat) {
        return chatMapper.selectChatList(chat);
    }

    /**
     * 新增会话
     *
     * @param chat 会话
     * @return 结果
     */
    @Override
    public int insertChat(Chat chat) {
        chat.setCreateTime(DateUtils.getNowDate());
        return chatMapper.insertChat(chat);
    }

    /**
     * 修改会话
     *
     * @param chat 会话
     * @return 结果
     */
    @Override
    public int updateChat(Chat chat) {
        chat.setUpdateTime(DateUtils.getNowDate());
        return chatMapper.updateChat(chat);
    }

    /**
     * 批量删除会话
     *
     * @param ids 需要删除的会话主键
     * @return 结果
     */
    @Override
    public int deleteChatByIds(Long[] ids) {
        return chatMapper.deleteChatByIds(ids);
    }

    /**
     * 删除会话信息
     *
     * @param id 会话主键
     * @return 结果
     */
    @Override
    public int deleteChatById(Long id) {
        return chatMapper.deleteChatById(id, CommonUtil.getUserIdNotNull());
    }

    /**
     * 列表
     *
     * @param condition 条件
     * @return {@link List< ChatAppVO>}
     * @author gdk
     * @since 2023/5/16 16:51
     */
    @Override
    public TableDataInfo appList(Chat condition) {
        condition.setCreateBy(CommonUtil.getStrUserIdNotNull());

        List<Chat> list = chatMapper.appList(condition);

        return new TableDataInfo(list, ChatStruct.INSTANCE.list2AppVOList(list));
    }

    /**
     * 修改会话
     *
     * @param dto dto
     * @author gdk
     * @since 2023/5/20 12:42
     */
    @Override
    public void edit(ChatEditDTO dto) {
        Chat chat = ChatStruct.INSTANCE.editDTO2Entity(dto);
        commonUtil.initUpdateAbout(chat);
        if (chatMapper.updateChat(chat) != 1) {
            CommonUtil.throwError();
        }
    }

    /**
     * 删除会话
     *
     * @param id id
     * @author gdk
     * @since 2023/5/16 17:28
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void remove(Long id) {
        Chat chatCondition = new Chat();
        chatCondition.setId(id);
        commonUtil.initUpdateAbout(chatCondition);
        chatCondition.setCreateBy(chatCondition.getUpdateBy());
        // 确保是本人数据，并删除会话
        if (chatMapper.removeLogic(chatCondition) != 1) {
            CommonUtil.throwError();
        }

        // 删除会话记录
        ChatMessage msgCondition = new ChatMessage();
        msgCondition.setUpdateTime(chatCondition.getUpdateTime());
        msgCondition.setUpdateBy(chatCondition.getUpdateBy());
        chatMessageMapper.removeByChatIdList(msgCondition, Collections.singletonList(id));
    }

    /**
     * 删除所有会话
     *
     * @author gdk
     * @since 2023/5/16 17:28
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void removeAll() {
        // 查询已有会话，防止执行较慢，导致删除会话记录时，删掉新的会话的记录
        Chat chatCondition = new Chat();
        commonUtil.initUpdateAbout(chatCondition);
        chatCondition.setCreateBy(chatCondition.getUpdateBy());
        List<Long> idList = chatMapper.idList(chatCondition);
        if (idList.isEmpty()) {
            return;
        }

        // 删除已有会话
        chatMapper.removeLogicByIdList(chatCondition, idList);

        ChatMessage msgCondition = new ChatMessage();
        msgCondition.setUpdateTime(chatCondition.getUpdateTime());
        msgCondition.setUpdateBy(chatCondition.getUpdateBy());
        // 删除已有会话的记录
        chatMessageMapper.removeByChatIdList(msgCondition, idList);
    }
}
